<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>基本管理</el-breadcrumb-item>
          <el-breadcrumb-item active>客户管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 添加按钮 -->
      <el-button
        type="success"
        style="margin-top: 30px"
        @click="addDialogVisible = true"
        >添加客户</el-button
      >
      <!-- 搜索 -->
      <div class="search_box">
        <div class="search">
          <el-input
            v-model="search.left"
            placeholder="请输入用户名称"
          ></el-input>
          <el-input
            v-model="search.right"
            placeholder="请输入联系人"
          ></el-input>
          <el-button plain @click="searchclick">搜索</el-button>
          <el-button type="warning" plain @click="clearinput">重置</el-button>
        </div>
      </div>
      <el-table
        :data="datalist"
        height="500"
        row-key="cid"
        border
        style="width: 100%; margin-top: 30px; height: 100%"
      >
        <el-table-column prop="cid" label="用户编号" width="180">
        </el-table-column>
        <el-table-column prop="client_name" label="用户名称" width="180">
        </el-table-column>
        <el-table-column prop="client_address" label="用户地址">
        </el-table-column>
        <el-table-column prop="client_person" label="联系人"> </el-table-column>
        <el-table-column prop="client_phone" label="联系电话">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="editDialog(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteDialog(scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加按钮 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名称" prop="pass">
          <el-input v-model="form.client_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户地址" prop="pass">
          <el-input v-model="form.client_address" autocomplete="off"  clearable :maxlength="0">
            <i slot="suffix" class="el-icon-location-outline" @click="openmap"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="pass">
          <el-input v-model="form.client_person" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="pass">
          <el-input v-model="form.client_phone" autocomplete="off"></el-input>
        </el-form-item>
        <div v-if="map" class="mapbox">
        <v-map  class="baidumap" @childMarker="changemarker"></v-map>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="addclient">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改按钮 -->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名称" prop="pass">
          <el-input v-model="form.client_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户地址" prop="pass">
          <el-input v-model="form.client_address" autocomplete="off"  clearable :maxlength="0">
            <i slot="suffix" class="el-icon-location-outline" @click="openmap"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="pass">
          <el-input v-model="form.client_person" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="pass">
          <el-input v-model="form.client_phone" autocomplete="off"></el-input>
        </el-form-item>
        <div v-if="map" class="mapbox">
        <v-map  class="baidumap" @childMarker="changemarker"></v-map>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="editClient">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addclient, editclient, delclient, searchclient } from "../../http/api";
import { mapActions, mapGetters } from "vuex";
// import Search from '../../components/search/search.vue'
import vMap from "../../components/Baidu/baidu.vue";
export default {
  data() {
    return {
      // datalist: [],
      //   添加提示框
      map: false,
      addDialogVisible: false,
      editDialogVisible: false,
      form: {
        cid: "",
        client_name: "",
        client_address: "",
        client_person: "",
        client_phone: null,
        lat:null,
        lng:null,
        is_del: 0,
      },
      search: {
        left: "",
        right: "",
      },
    };
  },
    components: {
    vMap,
    // BaiduMap,
  },
  created() {
    this.getclientlist();
  },
  methods: {
    ...mapActions({
      getclientlist: "client/getclientlistActions",
    }),
    //添加用户
    addclient() {
      let that = this;
      addclient(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getclientlist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    // 点击修改按钮
    editDialog(data) {
      this.editDialogVisible = true;
      this.form = {
        cid: data.cid,
        client_name: data.client_name,
        client_address: data.client_address,
        client_person: data.client_person,
        client_phone: data.client_phone,
        is_del: data.is_del,
        lat:data.lat,
        lng:data.lng,
      };
    },
    // 确认修改
    editClient() {
      let that = this;
      editclient(this.form).then((res) => {
        if (res.status == 201) {
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getclientlist();
        } else {
          that.$message.error(res.message);
        }
      });

      this.clearDialog();
    },
    // 删除数据
    deleteDialog(data) {
      let that = this;
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delclient(data).then((res) => {
            if (res.status == 201) {
              //   that.datalist = res.data;
              that.$message.success(res.message);
              that.getclientlist();
            } else {
              console.log(res);
              that.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 搜索用户
    searchclick() {
      // console.log(this.search);
      let that = this;
      searchclient(this.search).then((res) => {
        if (res.status == 201) {
          console.log(res);
          //   that.datalist = res.data;
          that.$message.success(res.message);
          that.$store.commit("client/changeclientlist",res.data)
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      });

      // this.clearDialog();
    },
    // 清空搜索
    clearinput() {
      this.search = {
        left: "",
        right: "",
      };
      this.getclientlist()
    },
    //清空数据
    clearDialog() {
      this.editDialogVisible = false;
      this.addDialogVisible = false;
      this.form = {
        cid: "",
        client_name: "",
        client_address: "",
        client_person: "",
        client_phone: null,
        lat:null,
        lng:null,
        is_del: 0,
      };
      this.closemap()
    },
        //打开地图
    openmap() {
      this.map = !this.map;
    },
    closemap(){
      this.map = false;
    },
    //获取地址坐标
    changemarker(data){
      console.log(data,'我是父组件');
      this.form.client_address=data.address+'('+data.title+')'
      this.form.lat=data.lat,
      this.form.lng=data.lng,
      this.closemap()
    }
  },
  computed: {
    ...mapGetters({
      datalist: "client/datalist",
    }),
  },
};
</script>

<style lang="less" scoped>
.search {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .el-input {
    width: 50%;
    margin-right: 10px;
  }
  margin-bottom: 10px;
}
.search_box {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.bm-view {
  width: 100%;
  height: 100%;
}
.el-form {
  position: relative;
}
.baidumap {

}
.mapbox{
  width: 550px;
  height: 500px;
  padding: 10px;
  position: absolute;
  top: -200px;
  right: -580px;
  // margin-left: 500px;
  background-color: #fff;
  border-radius: 10px;
}
</style>